package org.ptkparty.shared.model;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;

import org.datanucleus.api.jpa.annotations.Extension;
import org.ptkparty.server.dao.EMFService;

import com.google.appengine.api.users.UserServiceFactory;

@Entity
public class ResultEntity {

	public ResultEntity() {
	}

	public ResultEntity(ResultEntity toCopy) {
		user = toCopy.getUser();
		championship = toCopy.getChampionship();
		points = toCopy.getPoints();
		gameNumber = toCopy.getGameNumber();
		winGames = toCopy.getWinGames();
		looseGames = toCopy.getLooseGames();
		fannyWinGames = toCopy.getFannyWinGames();
		fannyLooseGames = toCopy.getFannyLooseGames();
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Version
	private Integer version;

	private String user;

	private String championship;

	private Double points;

	private Integer gameNumber;

	private Integer winGames;

	private Integer looseGames;

	private Integer fannyWinGames;

	private Integer fannyLooseGames;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public void setUser(UserEntity user) {
		this.user = user.getId();
	}

	public String getChampionship() {
		return championship;
	}

	public void setChampionship(String championship) {
		this.championship = championship;
	}

	public Double getPoints() {
		return points;
	}

	public void setPoints(Double points) {
		this.points = points;
	}

	public Integer getWinGames() {
		return winGames;
	}

	public void setWinGames(Integer winGames) {
		this.winGames = winGames;
	}

	public Integer getLooseGames() {
		return looseGames;
	}

	public void setLooseGames(Integer looseGames) {
		this.looseGames = looseGames;
	}

	public Integer getFannyWinGames() {
		return fannyWinGames;
	}

	public void setFannyWinGames(Integer fannyWinGames) {
		this.fannyWinGames = fannyWinGames;
	}

	public Integer getFannyLooseGames() {
		return fannyLooseGames;
	}

	public void setFannyLooseGames(Integer fannyLooseGames) {
		this.fannyLooseGames = fannyLooseGames;
	}

	public Integer getGameNumber() {
		return gameNumber;
	}

	public void setGameNumber(Integer gameNumber) {
		this.gameNumber = gameNumber;
	}

	public void persist() {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			em.persist(this);
		} finally {
			em.close();
		}
	}

	public void remove() {
		if (this.getId() != null) {
			EntityManager em = EMFService.get().createEntityManager();
			try {
				em.remove(this);
			} finally {
				em.close();
			}
		}
	}

	public static ResultEntity findResultEntity(String id) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			return em.find(ResultEntity.class, id);
		} finally {
			em.close();
		}
	}

	public static ResultEntity findLast(String championship, String user) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			List<ResultEntity> results = em.createQuery("SELECT r FROM ResultEntity r WHERE r.user=:user AND r.championship=:championship ORDER BY r.gameNumber DESC", ResultEntity.class) //
					.setParameter("user", user) //
					.setParameter("championship", championship) //
					.getResultList();

			if (results != null && !results.isEmpty()) {
				return results.get(0);
			} else {
				return null;
			}
		} finally {
			em.close();
		}
	}

	public static ResultEntity findSecondToLast(String championship, String user) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			List<ResultEntity> results = em.createQuery("SELECT r FROM ResultEntity r WHERE r.user=:user AND r.championship=:championship ORDER BY r.gameNumber DESC", ResultEntity.class) //
					.setParameter("user", user) //
					.setParameter("championship", championship) //
					.getResultList();

			if (results != null && results.size() >= 2) {
				return results.get(1);
			} else {
				return null;
			}
		} finally {
			em.close();
		}
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getUser() {
		return this.user;
	}

	public static List<ResultEntity> getAllForCurrentUser(String currentChampionship) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			List<ResultEntity> results = em.createQuery("SELECT r FROM ResultEntity r WHERE r.user=:user AND r.championship=:championship ORDER BY r.gameNumber ASC", ResultEntity.class) //
					.setParameter("user", UserEntity.getUser(UserServiceFactory.getUserService().getCurrentUser().getEmail()).getId()) //
					.setParameter("championship", currentChampionship) //
					.getResultList();

			results.size();

			return results;
		} finally {
			em.close();
		}
	}

	public static List<ResultEntity> getAllForChampionship(String currentChampionship) {
		EntityManager em = EMFService.get().createEntityManager();
		try {
			List<ResultEntity> results = em.createQuery("SELECT r FROM ResultEntity r WHERE r.championship=:championship ORDER BY r.gameNumber ASC", ResultEntity.class) //
					.setParameter("user", UserEntity.getUser(UserServiceFactory.getUserService().getCurrentUser().getEmail()).getId()) //
					.setParameter("championship", currentChampionship) //
					.getResultList();

			results.size();

			return results;
		} finally {
			em.close();
		}
	}

}